/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.on.clinical.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author Raphael
 */
public class ConexaoMysql {
    private Connection conexao;
    private Statement stmt;
    private ResultSet resultado;
    private String servidor;
    private String bancoDados;
    private String usuario;
    private String senha;

    
    public ConexaoMysql() throws Exception {
            
            servidor = "localhost";
            bancoDados = "jdbc:mysql://localhost:3306/Clinical";
            this.usuario = "root";
            this.senha = "root";
                   
       try{
            conectar(servidor, bancoDados, usuario, senha);
       }catch (Exception ex){
           throw ex;
       }
           
    }
    
    public boolean conectar(String servidor, String bancoDados, String usuario, String senha) throws Exception {
        try {
                Class.forName("com.mysql.jdbc.Driver)");
            
        } catch (Exception ex) {
            System.out.println("SQLException: " + ex.getMessage());

        }

        try {

            conexao = DriverManager.getConnection(bancoDados, usuario, senha);
            stmt = conexao.createStatement();


        } catch (SQLException ex) {

            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
            if (ex.getErrorCode() == 1017) {
                throw new Exception();
            } else {
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Estocar", JOptionPane.ERROR_MESSAGE);
            }
        }
        return true;

    }

    public boolean atualizar(String comando) throws SQLException, Exception {
        try {
            System.out.println(comando);
            stmt.executeUpdate(comando);
            return true;
        } catch (SQLException e) {
            if (e.getErrorCode() == 2292) {              
                throw new Exception("Não pode ser removido");                
            }else
                e.printStackTrace();
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public ResultSet consultar(String query) {
        try {
            System.out.println(query);
            resultado = stmt.executeQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultado;
    }

    public void close() {
        try {
            stmt.close();
        } catch (SQLException Ex) {
            stmt = null;
        }
        try {
            conexao.close();
        } catch (SQLException sqlEx) {
            conexao = null;
        }
    }
    
}
